package cn.psh.org.controller;


import cn.psh.basic.util.PageList;
import cn.psh.basic.util.AjaxResult;
import cn.psh.org.domain.Type;
import cn.psh.org.query.TypeQuery;
import cn.psh.org.service.impl.TypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/type")
@RestController
public class TypeController {

    @Autowired
    private TypeServiceImpl typeService;

    @GetMapping("/{id}")
    public Type queryById(@PathVariable Long id) {
        return typeService.queryById(id);
    }

    /*
    查询所有
     */
    @GetMapping
    public List<Type> queryAll() {
        return typeService.queryAll();
    }

    /**
     * 添加或修改数据
     */
    @PutMapping
    public AjaxResult addAAndUpdate(@RequestBody Type type) {

        try {
            if (null == type.getId()) {
                // 添加
                typeService.save(type);
            } else {
                // 修改
                typeService.update(type);
            }
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.OJBK().setMessage("系统繁忙，骚后重试！！");
    }

    /**
     * 根据id删除一个数据
     */
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){

        try {
            typeService.del(id);
            return AjaxResult.OJBK();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.OJBK().setMessage("系统繁忙，骚后重试！！");
    }

    @PostMapping
    public PageList<Type> queryByPage(@RequestBody TypeQuery query){
        return typeService.queryData(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            typeService.batchDel(ids);
            return AjaxResult.OJBK();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统错误！！请骚后重试！");
        }

    }
}

